package test;

import utils.sl651.SL651GenMsgUtil;
import utils.sl651.SL651CrcUtil;

/**
 * SL651 ASCII编码报文生成测试类
 * 用于验证ASCII编码报文生成的正确性
 */
public class SL651AsciiMessageTest2 {
    public static void main(String[] args) {
        System.out.println("测试生成ASCII编码的测试报文:");
        
        try {
            // 生成ASCII编码的测试报文
            String asciiTestMessage = SL651GenMsgUtil.generateAsciiTestMessage();
            System.out.println("生成的ASCII测试报文: " + asciiTestMessage);
            System.out.println("报文长度: " + asciiTestMessage.length() + " 字符");
            
            // 验证CRC
            boolean isCrcValid = SL651CrcUtil.verifyAscillMessageCrc(asciiTestMessage);
            System.out.println("CRC校验结果: " + (isCrcValid ? "通过" : "失败"));
            
            if (!isCrcValid) {
                // 如果CRC校验失败，重新计算并显示正确的CRC
                String dataPart = asciiTestMessage.substring(0, asciiTestMessage.length() - 4);
                String correctCrc = SL651CrcUtil.calculateCrc16ForAscii(dataPart);
                System.out.println("正确的CRC应该是: " + correctCrc);
                System.out.println("数据部分长度: " + dataPart.length() + " 字符");
            }
            
        } catch (Exception e) {
            System.err.println("生成报文时发生错误: " + e.getMessage());
            e.printStackTrace();
        }
        
        System.out.println("\n测试生成ASCII编码的定时报文:");
        try {
            // 生成ASCII编码的定时报文
            String asciiTimerMessage = SL651GenMsgUtil.generateAsciiTimerMessage();
            System.out.println("生成的ASCII定时报文: " + asciiTimerMessage);
            System.out.println("报文长度: " + asciiTimerMessage.length() + " 字符");
            
            // 验证CRC
            boolean isCrcValid = SL651CrcUtil.verifyAscillMessageCrc(asciiTimerMessage);
            System.out.println("CRC校验结果: " + (isCrcValid ? "通过" : "失败"));
            
            if (!isCrcValid) {
                // 如果CRC校验失败，重新计算并显示正确的CRC
                String dataPart = asciiTimerMessage.substring(0, asciiTimerMessage.length() - 4);
                String correctCrc = SL651CrcUtil.calculateCrc16ForAscii(dataPart);
                System.out.println("正确的CRC应该是: " + correctCrc);
                System.out.println("数据部分长度: " + dataPart.length() + " 字符");
            }
        } catch (Exception e) {
            System.err.println("生成报文时发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
}